package com.framework.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.framework.entity.AoUserEntity;
import com.framework.service.AoUserService;
import com.framework.utils.ExcelUtils;
import com.framework.utils.PageUtils;
import com.framework.utils.R;

import jxl.Cell;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
 * 刷币用户信息表
 * 
 * @author R & D
 * @email 908350381@qq.com
 * @date 2018-08-26 04:09:26
 */
@Controller
@RequestMapping("aouser")
public class AoUserController {
	@Autowired
	private AoUserService aoUserService;

	@RequestMapping("/aouser.html")
	public String list() {
		return "aouser/aouser.html";
	}

	@RequestMapping("/aouser_add.html")
	public String add() {
		return "aouser/aouser_add.html";
	}

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("aouser:list")
	public R list(Integer page, Integer limit) {
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);

		// 查询列表数据
		List<AoUserEntity> aoUserList = aoUserService.queryList(map);
		int total = aoUserService.queryTotal(map);

		PageUtils pageUtil = new PageUtils(aoUserList, total, limit, page);

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{id}")
	@RequiresPermissions("aouser:info")
	public R info(@PathVariable("id") Integer id) {
		AoUserEntity aoUser = aoUserService.queryObject(id);

		return R.ok().put("aoUser", aoUser);
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("aouser:save")
	public R save(@RequestBody AoUserEntity aoUser) {
		aoUserService.save(aoUser);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("aouser:update")
	public R update(@RequestBody AoUserEntity aoUser) {
		aoUserService.update(aoUser);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("aouser:delete")
	public R delete(@RequestBody Integer[] ids) {
		aoUserService.deleteBatch(ids);

		return R.ok();
	}

	@RequestMapping(value = "/uploadCSV", method = RequestMethod.POST)
	@ResponseBody
	public R upload(@RequestParam("file") MultipartFile sourceFile, MultipartHttpServletRequest request,
			HttpServletResponse response) throws IOException {
		MultipartFile file = request.getFile("file");
		Workbook workbook;
		try {
			workbook = Workbook.getWorkbook(file.getInputStream());
			// 遍历Sheet页
			Arrays.stream(workbook.getSheets()).forEach(sheet -> {
				int size = sheet.getRows();
				for (int i = 1; i < size; i++) {
					// 遍历每一行，读取每列信息
					AoUserEntity t = new AoUserEntity();
					Cell[] cs = sheet.getRow(i);
					for (int j=0; j< cs.length; j++) {
						t.setEmail(cs[0].getContents());
						t.setPassword(cs[1].getContents());
						t.setNodePepleCount(Integer.valueOf(cs[2].getContents()));
						t.setUsername(cs[3].getContents());
						t.setPhone(cs[4].getContents());
						t.setEth(cs[5].getContents());
					}
					aoUserService.save(t);
				}
			});
		} catch (BiffException e) {
			e.printStackTrace();
			return R.error();
		}
		
		return R.ok();
	}

	@RequestMapping(value = "/downloadCSV", method = RequestMethod.POST)
	@ResponseBody
	public String download(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Content-Disposition", "attachment; filename=template.xls");
		WritableWorkbook writableWorkbook;
		try {
			writableWorkbook = ExcelUtils.createTemplate(response.getOutputStream());
			writableWorkbook.write();
			writableWorkbook.close();
		} catch (WriteException e) {
			e.printStackTrace();
		}
		return null;
	}

}
